import pandas as pd

df = pd.read_csv('过滤.csv', encoding='gbk')

def toRowDf(row):
    rowDf = row.to_frame()
    rowDf = pd.DataFrame(rowDf.values.T, columns = rowDf.index)
    return rowDf

def setMeanNaN(meanSeries, oldSeries):
    allKey = oldSeries.index
    meanKey = meanSeries.index
    for i in allKey:
        if (not i in meanKey) or pd.isnull(meanSeries[i]):
            meanSeries[i] = oldSeries[i]
    return meanSeries

DfMap = {}
for index, row in df.iterrows():
    RID = row['RID']
    if not RID in DfMap.keys():
        print(RID)
        DfMap[RID] = pd.DataFrame(columns=df.columns)
    DfMap[RID] = pd.concat([DfMap[RID], toRowDf(row)])

df2 = pd.DataFrame(columns=df.columns)
for i in DfMap.values():
    print(i.iloc[0]['RID'])
    if i.shape[0]==1:
        df2 = pd.concat([df2, i])
    else:
        retData = i.mean()
        retData = setMeanNaN(retData, i.iloc[0])
        df2 = pd.concat([df2, toRowDf(retData)])

df2.to_csv('合并RID.csv')